package com.hcy.app.utils

import com.google.gson.Gson
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import java.io.IOException

object HttpUtil{
    private val client = OkHttpClient()
    private val gson = Gson()
    private val JSON = "application/json; charset=utf-8".toMediaType()

    fun post(url: String,data: Any): String{
        val reqBody = gson.toJson(data).toRequestBody(JSON)
        val request = Request.Builder()
            .url(url)
            .post(reqBody)
            .build()
        client.newCall(request).execute().use { response ->
            if (!response.isSuccessful) throw IOException("Unexpected code $response")
            val responseData = response.body?.string()
            return responseData.toString()
        }
    }

}



